﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using DevExpress.LookAndFeel;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using System;
using XCI.Helper;
using XCI.Windows.Forms;

namespace XCI.Sys.Forms
{
    /// <summary>
    /// 
    /// </summary>
    public partial class FrmNewTheme : FrmBase
    {
        /// <summary>
        /// 
        /// </summary>
        public FrmNewTheme()
        {
            InitializeComponent();
        }

        private void InitData()
        {
            if (DesignMode) return;

            skinDropDownButtonItem1.Enabled = false;
            skinPaletteDropDownButtonItem1.Enabled = false;

            radioLookAndFeelStyle.Properties.Items.BeginUpdate();
            radioLookAndFeelStyle.Properties.Items.Add(new RadioGroupItem("WindowsXP", "Native"));
            var sz = Enum.GetNames(typeof(LookAndFeelStyle));
            foreach (string s in sz)
            {
                radioLookAndFeelStyle.Properties.Items.Add(new RadioGroupItem(s, s));
            }
            radioLookAndFeelStyle.Properties.Items.EndUpdate();
            SetControlStyle();

            radioLookAndFeelStyle.EditValue = UserLookAndFeel.Default.ActiveStyle.ToString();
            editFont.Text = ObjectHelper.GetObjectString(WindowsFormsSettings.DefaultFont);
            var themeSetting = ThemeSetting.GetThemeSetting();
            editUseDirectX.IsOn = themeSetting.UseDirectX;
        }

        private void SetControlStyle()
        {
            radioLookAndFeelStyle.BackColor = this.BackColor;
            radioLookAndFeelStyle.ForeColor = this.ForeColor;

            //standaloneBarDockControl1.BackColor = this.BackColor;
            //bar1.BarAppearance.Normal.BackColor = this.BackColor;
        }

        private void FrmNewTheme_Load(object sender, EventArgs e)
        {
            UserLookAndFeel.Default.StyleChanged += Default_StyleChanged;
            InitData();
        }

        private void Default_StyleChanged(object sender, EventArgs e)
        {
            SetControlStyle();
        }

        private void radioLookAndFeelStyle_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (radioLookAndFeelStyle.EditValue == null) return;

            skinDropDownButtonItem1.Enabled = false;
            skinPaletteDropDownButtonItem1.Enabled = false;

            string style = radioLookAndFeelStyle.EditValue.ToString();
            switch (style)
            {
                case "Flat":
                    UserLookAndFeel.Default.SetFlatStyle();
                    break;
                case "Office2003":
                    UserLookAndFeel.Default.SetOffice2003Style();
                    break;
                case "Style3D":
                    UserLookAndFeel.Default.SetStyle3D();
                    break;
                case "UltraFlat":
                    UserLookAndFeel.Default.SetUltraFlatStyle();
                    break;
                case "WindowsXP":
                    UserLookAndFeel.Default.SetWindowsXPStyle();
                    break;
                case "Skin":
                    skinDropDownButtonItem1.Enabled = true;
                    skinPaletteDropDownButtonItem1.Enabled = true;
                    UserLookAndFeel.Default.SetSkinStyle(UserLookAndFeel.Default.ActiveSkinName, UserLookAndFeel.Default.ActiveSvgPaletteName);
                    break;
            }
        }

        private void BtnSetFont_Click(object sender, EventArgs e)
        {
            fontDialog1.Font = DevExpress.Utils.AppearanceObject.DefaultFont;
            if (fontDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                var fontObj = fontDialog1.Font;
                editFont.Text = ObjectHelper.GetObjectString(fontObj);
                WindowsFormsSettings.DefaultFont = fontObj;
                WindowsFormsSettings.DefaultMenuFont = fontObj;
                WindowsFormsSettings.DefaultPrintFont = fontObj;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var themeSetting = ThemeSetting.GetThemeSetting();
            themeSetting.Style = UserLookAndFeel.Default.ActiveStyle.ToString();
            themeSetting.SkinName = UserLookAndFeel.Default.ActiveSkinName;
            themeSetting.SvgPaletteName = UserLookAndFeel.Default.ActiveSvgPaletteName;
            themeSetting.FontName = ObjectHelper.GetObjectString(WindowsFormsSettings.DefaultFont);
            themeSetting.UseDirectX = editUseDirectX.IsOn;
            ThemeSetting.SaveThemeSetting();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void FrmNewTheme_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            UserLookAndFeel.Default.StyleChanged -= Default_StyleChanged;
        }
    }
}